package com.yanqu.road.server.manger;

import com.yanqu.road.dao.impl.config.ConfigDaoImpl;
import com.yanqu.road.dao.impl.config.ConfigVersionCheckResultDaoImpl;
import com.yanqu.road.entity.servercenter.ConfigVersionCheckInfo;
import com.yanqu.road.entity.servercenter.ConfigVersionCheckResultData;
import com.yanqu.road.logic.config.GameConfig;
import com.yanqu.road.logic.thread.ThreadTaskManger;
import com.yanqu.road.server.TempMgr;
import com.yanqu.road.utils.executor.GeneralTask;
import com.yanqu.road.utils.string.StringUtils;

import java.util.*;

public class CheckConfigVersionMgr extends TempMgr {
    private static final CheckConfigVersionMgr instance = new CheckConfigVersionMgr();

    public static CheckConfigVersionMgr getInstance() {
        return instance;
    }

    private static ThreadTaskManger threadTaskManger;

    @Override
    public boolean init() throws Exception {
        if (null == threadTaskManger) {
            threadTaskManger = new ThreadTaskManger(1, "CheckConfigVersionMgr");
        }
        return true;
    }

    @Override
    public boolean reloadConfig() throws Exception {
        return true;
    }

    @Override
    public boolean reloadData() throws Exception {
        return true;
    }

    @Override
    public boolean stop() {
        return true;
    }

    @Override
    public boolean save() {
        return true;
    }

    public static void checkConfigVersionUpgrade(int channelId, long serverId, int serverType, List<TempMgr> mgrList) {
        ConfigVersionCheckInfo configVersionCheckInfo = new ConfigDaoImpl().getConfigVersionCheckInfo(channelId);
        if (configVersionCheckInfo == null) {
            return;
        }

        ConfigVersionCheckResultData checkResultData = new ConfigVersionCheckResultDaoImpl().getConfigVersionCheckResultData(serverId, serverType);
        if (checkResultData != null && checkResultData.getConfigVersion().equals(configVersionCheckInfo.getConfigVersion())) {//版本已经更新过
            return;
        }

        if (checkResultData == null) {
            checkResultData = new ConfigVersionCheckResultData(channelId, serverId, serverType);
        }

        String lastReloadMgrName = "";
        try {
            Map<String, TempMgr> mgrMap = new HashMap<>();
            for (TempMgr tempMgr : mgrList) {
                String mgrSimpleName = tempMgr.getClass().getSimpleName();
                mgrMap.put(mgrSimpleName, tempMgr);
            }

            List<String> needReloadMgrNames = StringUtils.stringToStringList(configVersionCheckInfo.getMgrNames(), ",");
            for (String mgrName : needReloadMgrNames) {
                TempMgr tempMgr = mgrMap.get(mgrName);
                if (tempMgr == null) {
                    continue;
                }
                lastReloadMgrName = mgrName;
                tempMgr.reload();
            }
        } catch (Exception e) {
            getLogger().error("{},配置管理类重载失败!!,err={}", lastReloadMgrName, e);
            return;
        }


        checkResultData.setConfigVersion(GameConfig.CONFIG_VERSION);
        if (checkResultData.isInsertOption()) {
            new ConfigVersionCheckResultDaoImpl().add(checkResultData);
        } else {
            new ConfigVersionCheckResultDaoImpl().update(checkResultData);
        }
    }

    public static void addTask(GeneralTask task) {
        if (threadTaskManger == null) {
            return;
        }
        threadTaskManger.addTask(0, task);
    }
}
